package com.example.pcl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pcl.constant.MessageConstants;
import com.example.pcl.exception.ObjectException;
import com.example.pcl.mapper.HealthKnowledgeCommentsMapper;
import com.example.pcl.mapper.MentalHealthKnowledgeMapper;
import com.example.pcl.mapper.UsersMapper;
import com.example.pcl.pojo.dto.AdminCommentDTO;
import com.example.pcl.pojo.dto.AdminCommentFilterDTO;
import com.example.pcl.pojo.entity.HealthKnowledgeComments;
import com.example.pcl.pojo.vo.AdminCommentVO;
import com.example.pcl.result.PageResult;
import com.example.pcl.service.IHealthKnowledgeCommentsService;
import com.example.pcl.utils.ThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 健康知识评论表，记录用户对心理健康知识的评论 服务实现类
 * </p>
 *
 * @author xx
 * @since 2024-12-17
 */
@Service
public class HealthKnowledgeCommentsServiceImpl extends ServiceImpl<HealthKnowledgeCommentsMapper, HealthKnowledgeComments> implements IHealthKnowledgeCommentsService {


    @Override
    public PageResult<AdminCommentVO> listComments(@NotNull AdminCommentFilterDTO filterDTO) {

        // 执行分页查询
        IPage<AdminCommentVO> resultPage = baseMapper.listComments(new Page<>(filterDTO.getPageNum(), filterDTO.getPageSize()), filterDTO);

        // 返回分页结果
        return new PageResult<>(resultPage.getTotal(), resultPage.getRecords());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addComment(AdminCommentDTO commentDTO) {
        HealthKnowledgeComments comment = new HealthKnowledgeComments();
        BeanUtils.copyProperties(commentDTO, comment);
        Integer id = ThreadLocalUtil.getId();
        comment.setUserId(id);
        int rows = baseMapper.insert(comment);
        if (rows <= 0) {
            throw new ObjectException(MessageConstants.ADD_ERR);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateComment(AdminCommentDTO commentDTO) {
        HealthKnowledgeComments comment = new HealthKnowledgeComments();
        BeanUtils.copyProperties(commentDTO, comment);
        int rows = baseMapper.updateById(comment);
        if (rows <= 0) {
            throw new ObjectException(MessageConstants.UPDATE_ERR);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteComments(List<Integer> commentIds) {
        if (CollectionUtil.isNotEmpty(commentIds)) {
            List<Integer> ids = commentIds.stream().distinct().toList();
            if (!removeBatchByIds(ids)) {
                throw new ObjectException(MessageConstants.DELETE_ERR);
            }
        } else {
            throw new ObjectException(MessageConstants.DELETE_LIST_NULL);
        }
    }

    @Override
    public AdminCommentVO finById(Integer id) {
        return baseMapper.findById(id);
    }


}
